package br.com.academia.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.academia.modelo.Matricula;

public class MatriculaDAO {

	private Connection connection;
	private PreparedStatement stmt;
	private ResultSet rs;
	private String sql;
	private boolean resultado = true;

	public MatriculaDAO() {
		connection = new ConnectionFactory().getConnection();
	}

	public boolean incluir(Matricula matricula) {
		sql = "insert into matricula (nome , cpf, email, nascimento, telefone, sexo) "
				+ "values (?,?,?,?,?,?)";
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setString(1, matricula.getNome());
			stmt.setInt(2, matricula.getCpf());
			stmt.setString(3, matricula.getEmail());
			stmt.setString(4, matricula.getNascimento());
			stmt.setInt(5, matricula.getTelefone());
			stmt.setString(6, matricula.getSexo());
			stmt.executeUpdate();
		} catch (SQLException e) {
			resultado = false;
			throw new RuntimeException(e);
		}
		close();
		return resultado;
	}

	private void close() {
		try {
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public boolean alterar(Matricula matricula) {
		sql = "update matricula set nome=?, cpf=?, email=?, nascimento=?, telefone=?, sexo=? where idmatricula=?";
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setString(1, matricula.getNome());
			stmt.setInt(2, matricula.getCpf());
			stmt.setString(3, matricula.getEmail());
			stmt.setString(4, matricula.getNascimento());
			stmt.setInt(5, matricula.getTelefone());
			stmt.setString(6, matricula.getSexo());
			stmt.setInt(7, matricula.getIdmatricula());
			stmt.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		close();
		return resultado;
	}

	public boolean excluir(Matricula matricula) {
		sql = "delete from matricula where idmatricula = ?";
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setInt(1, matricula.getIdmatricula());
			stmt.execute();
		} catch (SQLException e) {
			resultado = false;
			throw new RuntimeException(e);
		}
		close();
		return resultado;
	}

	public Matricula consultar(Matricula matricula) {
		sql = "select nome , cpf, email, nascimento, telefone, sexo from matricula where idmatricula=?";
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setInt(1, (int) matricula.getIdmatricula());
			rs = stmt.executeQuery();
			rs.next();
			matricula.setNome(rs.getString("nome"));
			matricula.setCpf(rs.getInt("cpf"));
			matricula.setEmail(rs.getString("email"));
			matricula.setNascimento(rs.getString("nascimento"));
			matricula.setTelefone(rs.getInt("telefone"));
			matricula.setSexo(rs.getString("sexo"));
			rs.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		close();
		return matricula;
	}

	public ArrayList<Matricula> listar() {
		ArrayList<Matricula> lista = new ArrayList<>();
		sql = "select * from matricula";
		try {
			stmt = connection.prepareStatement(sql);
			rs = stmt.executeQuery();
			Matricula matricula;
			while (rs.next()) {
				matricula = new Matricula();
				matricula.setIdmatricula(rs.getInt("idmatricula"));
				matricula.setNome(rs.getString("nome"));
				matricula.setCpf(rs.getInt("cpf"));
				matricula.setEmail(rs.getString("email"));
				matricula.setNascimento(rs.getString("nascimento"));
				matricula.setTelefone(rs.getInt("telefone"));
				matricula.setSexo(rs.getString("sexo"));
				lista.add(matricula);
			}
			rs.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		close();
		return lista;
	}
}
